<template>
  <el-dialog
    class="custom-dialog"
    v-model="dialogVisible"
    title="关联地块"
    width="1200px"
    @close="handleClose"
  >
    <div class="custom-table-list-wrapper">
      <div class="search" :class="{ active: toggleSearchStatus }">
        <div class="search-left" ref="searchFromRef">
          <el-form class="search-form" inline>
            <el-form-item label="关键词">
              <el-input
                class="search-input"
                v-model="searchParams.certificate"
                placeholder="请输入产权证号"
                :suffix-icon="Search"
              ></el-input>
            </el-form-item>
          </el-form>
          <a
            class="search-left-toggle"
            v-if="showToggle"
            @click="handleToggleSearch"
          >
            <svg-icon :name="toggleSearchStatus ? 'up' : 'down'" />
            {{ toggleSearchStatus ? '收起' : '展开' }}
          </a>
        </div>
        <div class="search-right">
          <el-button type="primary" @click="initTableListAndSelect(1)"
            ><svg-icon name="search" size="14px" />搜索</el-button
          >
          <el-button @click="handleReset()"
            ><svg-icon name="reset" size="17px" />重置</el-button
          >
        </div>
      </div>
      <div class="content">
        <div class="content-table">
          <el-table
            class="custom-table"
            ref="tableRef"
            :data="tableData"
            @select="handleSelectOne"
          >
            <el-table-column type="selection" width="60" />
            <el-table-column prop="propertyOwner" label="产权人">
              <template #default="scope">
                {{
                  getDictValue('propertyUnit', scope.row.propertyOwner)
                }}</template
              >
            </el-table-column>
            <el-table-column prop="certificate" label="产权证号" />
            <el-table-column prop="name" label="宗地名称" />
            <el-table-column prop="area" label="土地面积(m²)" />
            <el-table-column prop="character" label="土地性质">
              <template #default="scope">
                {{ getDictValue('landNature', scope.row.character) }}
              </template>
            </el-table-column>
            <el-table-column prop="usage" label="土地用途">
              <template #default="scope">
                {{ getDictValue('landUse', scope.row.usage) }}
              </template>
            </el-table-column>
            <el-table-column prop="useStatus" label="使用情况">
              <template #default="scope">
                {{ getDictValue('landUsage', scope.row.useStatus) }}
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="content-page">
          <el-pagination
            class="custom-pagination"
            :current-page="paginationParams.pageIndex"
            :page-size="paginationParams.pageSize"
            background
            :page-sizes="[10, 20, 30, 50]"
            layout="total, prev, pager, next, sizes, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="initTableListAndSelect"
          />
        </div>
      </div>
    </div>
    <template #footer>
      <el-button @click="handleClose(false)">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref, onMounted, watch, nextTick } from 'vue'
import { useTableList } from '@/hooks/useTableList'
import { landPageList } from '@/api/manager/dataManage/park'
import { useDict } from '@/hooks/useDict'
const { getDictValue, getDictStyle } = useDict()
const searchParams = ref({
  parkId: '',
  keywords: '',
})
const {
  Search,
  toggleSearchStatus,
  showToggle,
  handleToggleSearch,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  handleReset,
  handleDelete,
  detailRef,
  currentDetail,
  searchFromRef,
} = useTableList(searchParams, landPageList)
const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {},
  },
  parkId: {
    type: String,
  },
})
const tableRef = ref()
const multipleSelection = ref([])
const emit = defineEmits(['update:modelValue'])
const dialogVisible = ref(false)
const handleClose = () => {
  if (
    multipleSelection.value.length > 0 &&
    multipleSelection.value[0].pkid !== props.modelValue.pkid
  ) {
    tableRef.value &&
      tableRef.value.toggleRowSelection(multipleSelection.value[0], false)
    multipleSelection.value = []
  }
  if (props.modelValue.pkid) {
    multipleSelection.value = [props.modelValue]
    tableRef.value &&
      tableRef.value.toggleRowSelection(multipleSelection.value[0], true)
  }
  dialogVisible.value = false
}
const handleSubmit = () => {
  dialogVisible.value = false
  emit(
    'update:modelValue',
    multipleSelection.value.length > 0 ? multipleSelection.value[0] : {},
  )
}
const initTableListAndSelect = async (pageIndex) => {
  await initTableList(pageIndex)
  nextTick(() => {
    if (multipleSelection.value.length > 0) {
      const row = tableData.value.find(
        (i) => i.pkid === multipleSelection.value[0].pkid,
      )
      if (tableRef.value) {
        tableRef.value.toggleRowSelection(row, true)
      }
    }
  })
}
watch(
  () => props.parkId,
  () => {
    if (props.parkId) {
      searchParams.value.parkId = props.parkId
      // initTableListAndSelect(1)
    }
  },
  {
    immediate: true,
    deep: true,
  },
)
watch(
  () => dialogVisible.value,
  async () => {
    if (dialogVisible.value) {
      initTableListAndSelect(1)
    }
  },
)
watch(
  () => props.modelValue,
  () => {
    if (!props.modelValue.pkid && multipleSelection.value.length === 0) {
      return
    }
    if (multipleSelection.value.length > 0) {
      const row = tableData.value.find(
        (i) => i.pkid === multipleSelection.value[0].pkid,
      )
      tableRef.value && row && tableRef.value.toggleRowSelection(row, false)
      multipleSelection.value = []
    }
    if (props.modelValue.pkid) {
      const row = tableData.value.find((i) => i.pkid === props.modelValue.pkid)
      if (tableRef.value && row) {
        tableRef.value.toggleRowSelection(row, true)
        multipleSelection.value.push(row)
      } else {
        multipleSelection.value.push(props.modelValue)
      }
    }
  },
  {
    immediate: true,
    deep: true,
  },
)
const handleSelectOne = (selection, row) => {
  if (selection && selection.length > 0) {
    if (multipleSelection.value.length > 0) {
      const oldRow = tableData.value.find(
        (i) => i.pkid === multipleSelection.value[0].pkid,
      )
      tableRef.value.toggleRowSelection(oldRow, false)
    }
    multipleSelection.value = [row]
  } else {
    tableRef.value.toggleRowSelection(row, false)
    multipleSelection.value = []
  }
}
defineExpose({ dialogVisible, searchParams, initTableListAndSelect })
onMounted(() => {})
</script>
<style lang="scss" scoped>
@import '@/assets/styles/table.scss';

:deep(.el-table__header) {
  .el-checkbox {
    display: none;
  }
}

.search {
  padding: 0;
  margin: 0;
  box-shadow: none;
  height: auto;
}

.content {
  padding: 0;
}
</style>
